<?php
namespace Admin\Controller;
use Common\Model\AgentModel;
use Think\AjaxPage;
use Think\Controller;

class AgentController extends CommonController
{
    public function _initialize()
    {
        $params = array('searchisset' ,'closed' ,'for_audit' ,'searchagent',"ajax_agent_list","ajax_change_list");
        parent::_initialize($params);
    }

    // 代理列表
    public function index()
    {
        $this->display();
    }

    // 已关代理列表
    public function closed()
    {
        $agent_d = D("AgentView");

        $where["agent.u_status"] = array("eq" ,0);
        if(isset($_REQUEST['mName']))
        {
            $mName = trim(I('mName'));
            if($mName != ''){
                $cWhere['agent.agent_name'] = array("like" ,"%$mName%");
                $cWhere['agent.mobile'] = array("like" ,$mName);
                $cWhere['_logic'] = 'or';
                $where['_complex'] = $cWhere;
            }
        }
        $count = $agent_d->where($where)->count();
        $page = set_page($count, C('PAGE_NUM'));
        $agents = $agent_d->where($where)->limit($page->firstRow.','.$page->listRows)
            ->order('reg_time DESC')->select();

        $this->ranks = M("agent_rank")->where("rank_status = 1")->order("rank_grade ASC")->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('agents',$agents);
        $this->display();
    }

    // 代理详情
    public function agent_view()
    {
        $agent_id = to_int(I('get.id'));
        $agent = D("AgentView")->where("agent.agent_id = $agent_id")->find();
        if(!$agent){
            $this->success("请求失败" ,$_SERVER['HTTP_REFERER'] ,1);
        }
        $count = M("recommended")->where("add_agent = $agent_id")->count();
        $page = set_page($count, 10);
        $recommend_agents = M("recommended r")
            ->join(C("DB_PREFIX")."agent a ON a.agent_id = r.beadd_agent" ,"LEFT")
            ->where("r.add_agent = $agent_id")->order("r.add_time DESC")
            ->field("r.*,a.agent_name")->limit($page->firstRow.','.$page->listRows)
            ->select();
        $show = $page->show();
        $this->rec_count = $count;
        $this->assign('page',$show);
        $this->assign("recommend_agents" ,$recommend_agents);
        // 代理的品牌
        $brands = M("agent_brand a")
            ->join(C("DB_PREFIX")."brand b ON a.brand_id = b.id" ,"LEFT")
            ->field("a.*,b.b_name")
            ->where("a.agent_id = $agent_id")->select();
        foreach($brands as $b){
            $brand_[] = "<span><strong class='red_common'>" .$b["b_name"] ."</strong>".
                "<strong class='red_common'>（". GetRankNameById($b["rank_id"]). "）</strong></span>";
        }
        $agent_brands = implode(" / " ,$brand_);
        $this->assign("agent_brands" ,$agent_brands);
        $agentAddress = D('Agent')->where( array('agent_id' => $agent_id) )->relation(true)->find();
        $this->assign('agentAddress', $agentAddress);

        $this->agent = $agent;
        $this->display();
    }

    /*
     * 代理升级机制介绍
     * 不管代理当前属于什么级别，知道是代理升级成功，当前下线代理还是属于自己下线代理，上线代理则更换为升级后级别的上线代理
     * 代理自己也可以添加代理，添加时为自己同等级代理，所属同一上级代理的下线，当自己升级时，自己所添加的所有代理都变成自己的下线代理
     * */

    // ajax 读取代理列表
    public function ajax_agent_list()
    {
        $where = '1 = 1 '; // 搜索条件
        $reg = to_int(I('reg'));
        if($reg != 0){
            if($reg == 1){
                $where = "$where AND invitation_code = '*'" ;
            }else{
                $where = "$where AND invitation_code <> '*'" ;
            }
        }
        (I('u_status') !== '') && $where = "$where AND u_status = ".I('u_status') ;
        // 关键词搜索
        $mName = I('mName') ? trim(I('mName')) : '';
        if($mName) $where = "$where AND (agent_name like '%$mName%' or mobile like '%$mName%')" ;
        $count = M('agent')->where($where)->count();
        $Page  = new AjaxPage($count, C('PAGE_NUM'));
        $show = $Page->show();
        $order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
        $agentList = M('agent')->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('agents',$agentList);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    // ajax 读取代理升级记录
    public function ajax_up_list()
    {
        $upgrade_d = D("UpgradeView");
        $where = '1 = 1 '; // 搜索条件
        // 关键词搜索
        $mName = I('mName') ? trim(I('mName')) : '';
        if($mName) $where = "$where AND (agent.agent_name like '%$mName%')" ;
        (I('st') != '') && $where = "$where AND agent_upgrade.is_adopt = ". I('st');
        $count = $upgrade_d->where($where)->count();
        $Page  = new AjaxPage($count, C('PAGE_NUM'));
        $show = $Page->show();
        $order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
        $upgrades = $upgrade_d->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('upgrades',$upgrades);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

}